package jpb.module.rest.follow;

import jpb.constant.Reply;
import jpb.constant.ReturnState;
import jpb.constant.StatusCode;
import jpb.module.daomain.organize.Organize;
import jpb.module.service.organize.OrganizeService;
import jpb.security.HandleCookie;
import jpb.security.HandleToken;
import jpb.util.RespUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by haol on 2017/5/9.
 */
@RestController
@RequestMapping("/api/app/follow")
public class OrganizeFollowController {

        @Autowired
        private OrganizeService organizeService;

        HandleToken handleToken = new HandleToken();
        /**
         *@api {POST} /api/app/follow/add 关注组织
         * @apiGroup FollowMem
         * @apiName addFollow
         * @apiDescription  登录用户关注id为oid 的公共组织
         * @apiParam {String} oid  公众组织id(not null)
         *  @apiParamExample {json} 请求样例：
         *  POST /api/app/follow/add
         *  {
         *            "oid":"1",
         *  }
         *  @apiSuccessExample {json} 返回样例:
         *  {
         *      "code":0,"msg":"关注成功"
         *  }
         */
        @RequestMapping(value = "/add", method = RequestMethod.POST)
        public String addFollow(String oid,HttpServletRequest request) {
                String token = HandleCookie.getValueByName(request, StatusCode.COOKIE_JWT_APP);
                String num = handleToken.getValueByKey(token, StatusCode.SUBJECT_KEY);
                Organize organize = organizeService.get(oid);
                if (organize == null){
                        return new Reply(1,"此组织不存在").toJson();
                }
                if(organize.getType() != 2){
                        return new Reply(1,"私有组织不能被关注").toJson();
                }

                if (organizeService.get(oid,num) != 0){
                        return new Reply(1,"你已经关注").toJson();
                }
                organizeService.add(oid,num);
                return new Reply(0,ReturnState.SUCCESS).toJson();
        }

        /**
         *@api {PUT} /api/app/follow/del 取消关注
         * @apiGroup FollowMem
         * @apiName delFollow
         * @apiDescription 登录用户取消对组织id为{id}的关注
         * @apiParam {String} oid 组织Id(not null)
         *  @apiParamExample {json} 请求样例：
         *  PUT /api/app/follow/del
         *  {
         *            "id":"1",
         *  }
         *  @apiSuccessExample {json} 返回样例:
         *  {
         *      "code":0,"msg":"更新成功"
         *  }
         */
        @RequestMapping(value = "/del", method = RequestMethod.POST)
        public String delFollow(String oid,HttpServletRequest request) {
                String token = HandleCookie.getValueByName(request, StatusCode.COOKIE_JWT_APP);
                String num = handleToken.getValueByKey(token, StatusCode.SUBJECT_KEY);
                organizeService.delete(oid,num);
                return new Reply(0,ReturnState.SUCCESS).toJson();
        }
        /**
         *@api {GET} /api/app/follow/org 获取关注的组织信息
         * @apiGroup FollowMem
         * @apiName getPubOrgByMem
         * @apiDescription  获取登录用户的关注的全部组织
         *  @apiParamExample {json} 请求样例：
         *  GET /api/app/follow/org
         *  @apiSuccessExample {json} 返回样例:
         *  {
         *    "code":0,"msg":"成功","data":[{"id":"d612462c-f02a-4ac7-8449-4ef20baa97e6","mark":"com-gcu-one","name":"华广常用电话","createTime":"Wed May 10 15:26:28 CST 2017","share":0}]
         *  }
         */
        @RequestMapping(value = "/org", method = RequestMethod.GET)
        public String getPubOrgByMem(HttpServletRequest request) {

                String token = HandleCookie.getValueByName(request, StatusCode.COOKIE_JWT_APP);
                String num = handleToken.getValueByKey(token, StatusCode.SUBJECT_KEY);
              //  String sid ="1";
                List<Organize> organizes = organizeService.getPubOrgByNum(num);
                Reply<List<Organize>> reply = new Reply<List<Organize>>(0, ReturnState.SUCCESS, organizes);

                return reply.toJson();
        }
        /**
         *@api {GET} /api/app/follow/allOrg 获取全部公共的组织信息
         * @apiGroup FollowMem
         * @apiName getPubOrg
         * @apiDescription  获取全部的公共组织信息
         *  @apiParamExample {json} 请求样例：
         *  GET /api/app/follow/allOrg
         *  @apiSuccessExample {json} 返回样例:
         *  {
         *     "code":0,"msg":"成功","data":[{"id":"d612462c-f02a-4ac7-8449-4ef20baa97e6","mark":"com-gcu-one","name":"华广常用电话","createTime":"Wed May 10 15:26:28 CST 2017","share":0}]
         *  }
         */
        @RequestMapping(value = "/allOrg", method = RequestMethod.GET)
        public String getPubOrg(HttpServletResponse response) {
                RespUtil.setACAO(response);

                List<Organize> organizes = organizeService.getAllPubOrg();

                Reply<List<Organize>> reply = new Reply<List<Organize>>(0, ReturnState.SUCCESS, organizes);

                return reply.toJson();
        }

}
